package carassius.DAL;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.AbstractMap.SimpleEntry;

/**
 *
 * @author siebz0r
 */
public class WaterMeasurementListRow
		extends Row<SimpleEntry<Integer, String>>
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private final WaterMeasurementRow _waterMeasurementRow;
	private final WaterConditionRow _waterConditionRow;
	private final String SQL_UPDATE_VALUE = "UPDATE water_measurement_list SET value = ? WHERE measurement_id = ? AND condition = ?";
	private final Object LOCK;
	private Double _value;
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public WaterMeasurementListRow(ResultSet resultSet) throws SQLException
	{
		super(new SimpleEntry<Integer, String>(resultSet.getInt("measurement_id"), resultSet.getString("condition")));
		_waterMeasurementRow = (WaterMeasurementRow) Database.getWaterMeasurementTable().getByPrimaryKey(resultSet.getInt("measurement_id"));
		_waterConditionRow = Database.getWaterConditionTable().getByPrimaryKey(resultSet.getString("condition"));
		_value = resultSet.getDouble("value");
		LOCK = new Object();
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Properties">
	public WaterConditionRow getWaterConditionRow()
	{
		return _waterConditionRow;
	}

	public WaterMeasurementRow getWaterMeasurementRow()
	{
		return _waterMeasurementRow;
	}

	public Double getValue()
	{
		synchronized(LOCK)
		{
			return _value;
		}
	}

	public boolean setValue(double value)
	{
		boolean result = false;
		synchronized (LOCK)
		{
			if (value != _value)
			{
				try
				{
					PreparedStatement st = Database.prepareStatement(SQL_UPDATE_VALUE);
					st.setDouble(1, value);
					st.setInt(2, _waterMeasurementRow.getId());
					st.setString(3, _waterConditionRow.getName());

					if (result = (st.executeUpdate() == 1))
					{
						_value = value;
					}
					st.close();
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Methods">
	@Override
	public boolean equals(Object obj)
	{
		if (obj == null)
		{
			return false;
		}
		if (getClass() != obj.getClass())
		{
			return false;
		}
		final WaterMeasurementListRow other = (WaterMeasurementListRow) obj;
		if (this._waterMeasurementRow != other._waterMeasurementRow && (this._waterMeasurementRow == null || !this._waterMeasurementRow.equals(other._waterMeasurementRow)))
		{
			return false;
		}
		if (this._waterConditionRow != other._waterConditionRow && (this._waterConditionRow == null || !this._waterConditionRow.equals(other._waterConditionRow)))
		{
			return false;
		}
		if (this._value != other._value && (this._value == null || !this._value.equals(other._value)))
		{
			return false;
		}
		return true;
	}

	@Override
	public int hashCode()
	{
		int hash = 3;
		return hash;
	}

	@Override
	public String toString()
	{
		return _waterConditionRow.toString();
	}
	//</editor-fold>
}
